// JavaScript Document
var getByAjax = function(){
	var ajax = function(){
		//参数说明：data为传送的数据该数据以问号的形式追加到url后面，method=get|post，way=true|false
		var data;
		var method = "GET";
		var way = true;
		this.setData = function(d){
			date = d;	
		}
		this.setMethod = function(m){
			method = m;	
		}
		this.setWay = function(w){
			way = w;	
		}
		this.init = function(d,m,w){
			data = d;
			method = m;
			way = w;		
		}
		//参数说明：访问的目标地址，f为回调函数
		this.get = function(url,f){
			if(url==null){
				return;	
			}
			if(url.indexOf("?")>0){
				url = url + createParameter();
			}
			else{
				url = url+"?1=1" + createParameter();
			}
			
			var xmlhttp = getXMLHttp();
			//url = encodeURIComponent(url);
			xmlhttp.open(method, url, way);
			xmlhttp.send(null);
			callBack(xmlhttp,f);
		}
		this.post = function(url,f){
			var xmlHttp = getXMLHttp();
			xmlHttp.open("POST",url,true);
			xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			xmlHttp.send(createParameter());
			callBack(xmlHttp,f);
		};
		var callBack = function(xmlhttp,f){
			xmlhttp.onreadystatechange = function() {
				if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
					if(f == null){
						return;
					}
					var result = xmlhttp.responseText;
		            
					f(result);
				}
			}
		};
		var getXMLHttp = function(){
			var xmlhttp;
			if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
				xmlhttp = new XMLHttpRequest();
			} else {// code for IE6, IE5
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}	
			//xmlhttp.setRequestHeader("contentType","text/html;charset=uft-8");
			return xmlhttp;
		}
		var createParameter = function(){
			var parameter = "";
			if(data != null&&data instanceof Array){
				for(var i =0;i<data.length;i++){
					/*if(i == 0){
						parameter = parameter +"?";
					}*/
					//else{
						parameter = parameter +"&";
					//}
					parameter = parameter+data[i].name+"="+encodeURIComponent(data[i].value);	
				}
			}
			return parameter;
		}
	}
	return new ajax();
}
function toJson(data,url){
	// 构造返回JSON对象的方法
	var func = null;
	try{
		func = new Function("return "+data);
	}
	catch(err){
		if(url){
			redirect(url);
		}
		return;
	}
    // 得到JSON对象
    var json = func();
    return json;
}
function redirect(url){
	var win = window;
	while(win.parent&&win != win.parent){
		win = win.parent;
	}
	win.location = url;
}